#ifndef PAINTDATA_H
#define PAINTDATA_H
#include <QDebug>
#include <QQueue>
#include <algorithm>
#include <QList>
enum Axis{
    EX = 1,
    EY = 2,
    EZ = 3
};

class Point2d{
public:
    Point2d():x(0), y(0){ }
    Point2d(float x, float y) : x(x), y(y){ }
public:
    float x;
    float y;
};

class Point3d{
public:
    Point3d():x(0), y(0), z(0){ }
    Point3d(float x, float y, float z) : x(x), y(y), z(z){ }
public:
    float x;
    float y;
    float z;
};

class CDataCell{
public:
    CDataCell() {
        val = 0.f;
        curTime = 0;
        isPaint =true;
        mColor[0] = 0;
        mColor[1] = 0;
        mColor[2] = 0;
    }
    CDataCell* copy(CDataCell& cell) {
        this->val = cell.val;
        this->curTime = cell.curTime;
        this->isPaint = cell.isPaint;
        this->mColor[0] = cell.mColor[0];
        this->mColor[1] = cell.mColor[1];
        this->mColor[2] = cell.mColor[2];
        return this;
    }
    virtual ~CDataCell() {
        curTime = 0;
        isPaint = false;
    }
public:
    long curTime;
    float val;
    bool  isPaint; //是否绘制
    float mColor[3];
};

class CData{
public:
    CData(int max){
        mMaxPeriod = max;
        head = 0;
        tail = -1;
        queueCnt = 0;
        //预分配内存
        datas = new CDataCell*[mMaxPeriod];
    }
    void setData(CDataCell& cell) {
        if (queueCnt == mMaxPeriod) {
            //移除队列头数据
            datas[head++]->copy(cell);
            tail = head - 1;
            if(head >= mMaxPeriod)  {
                head = 0;
            }
        }
        else {
            datas[queueCnt++] = (new CDataCell())->copy(cell);
            tail = queueCnt - 1;
        }
    }
    long getOffset() {
        if (datas[head] != NULL) return datas[head]->curTime;
        return 0;
    }
    int Head() {
        return this->head;
    }
    int Tail() {
        return this->tail;
    }
    int Count() {
        return this->queueCnt;
    }
    virtual ~CData() {
        for(int i = 0; i < mMaxPeriod; i++) {
            if(datas[i] != NULL)  {
                delete datas[i];
                datas[i] = NULL;
            }
        }
        delete datas;
        datas = NULL;
        queueCnt = 0;
        head = 0;
        tail = -1;
    }
public:
    CDataCell** datas;
private:
    int queueCnt;
    int mMaxPeriod;
    int head;
    int tail;
};

class CDataColor{
public:
    CDataColor(){}
    CDataColor(float min, float max, byte r, byte g, byte b) {
        minData = min;
        maxData = max;
        color[0] = r;
        color[1] = g;
        color[2] = b;
    }
public:
    float minData;
    float maxData;
    byte color[3]; //0 -255
};

class CColorSelector{
public:
    CColorSelector(){}
    virtual ~CColorSelector() {
        list.clear();
    }
    CColorSelector* set(float min, float max, byte r, byte g, byte b) {
        list.append(CDataColor(min, max, r, g, b));
        return this;
    }
    void valToColor(float val, float& r, float& g, float& b) {
        for (int i = 0; i < list.size(); ++i) {
            if(list[i].minData <= val && val <list[i].maxData) {
                r = float(list[i].color[0]) / 255.f;
                g = float(list[i].color[1]) / 255.f;
                b = float(list[i].color[2]) / 255.f;
                break;
            }
        }
    }
private:
    QList<CDataColor> list;
};

#endif // PAINTDATA_H
